<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use addons\epay\library\Service;
use app\api\validate\CaptainTeam;
use app\common\controller\Api;
use app\common\manager\CaptainManager;
use app\common\manager\notify\NotifyManager;
use app\common\manager\PayLogManager;
use app\common\model\PayLog;
use think\Config;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 回调处理
 */
class Notify extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 队长个人信息及中心信息
     */
    public function notifyWechat()
    {
        try {
            //file_put_contents('a.txt','callback start',FILE_APPEND);
            $pay = Service::checkNotify('wechat');
            if (!$pay) {
                //file_put_contents('a.txt','pay_false',FILE_APPEND);
                $this->error('失败',['code'=>'FAIL','message'=>'失败'],500);
            }
            //file_put_contents('a.txt','callback pay verify true',FILE_APPEND);
            $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
            //print_r($data);
            if (Service::isVersionV3()) {
                $data = $data['resource']['ciphertext'];
                $data['total_fee'] = $data['amount']['total'];
            }

            $out_trade_no = $data['out_trade_no'];
            $result_code = $data['result_code'];
            //file_put_contents('a.txt','$out_trade_no：'.$out_trade_no,FILE_APPEND);
            if (!$payLogInfo = PayLog::where('order', $out_trade_no)->find()) {
                throw new Exception('失败1');
            }

            if ($payLogInfo['processed']==PayLog::PROCESSED_STATUS_YES){
                //file_put_contents('a.txt','processed：'.$payLogInfo['processed'],FILE_APPEND);
                $this->success('成功',['code'=>'SUCCESS','message'=>'OK'],200);
            }

            //file_put_contents('a.txt','PayLog::update：start',FILE_APPEND);
            //file_put_contents('a.txt','PayLog11:'.$payLogInfo['id'],FILE_APPEND);
            $updateResult = PayLog::where('id',$payLogInfo['id'])->update(['response'=>serialize($data),'response_code'=>$result_code,'update_time'=>time()]);
            if(!$updateResult){
                //file_put_contents('a.txt','PayLog::update：fail',FILE_APPEND);
                throw new Exception('失败');
                //$this->error('失败',['code'=>'FAIL','message'=>'失败'],500);
            }
            //file_put_contents('a.txt','PayLog::update：suceess',FILE_APPEND);
            NotifyManager::notify($payLogInfo['method'],$data);
            $result = NotifyManager::getResult();
            //file_put_contents('a.txt','deal with result：'.json_encode($result),FILE_APPEND);
            if ($result['code']==NotifyManager::$successCode){
                $this->success('成功',['code'=>'SUCCESS','message'=>'OK'],200);
            }else{
                throw new Exception($result['message']);
            }

        }catch (Exception $e){
            //file_put_contents('callback_'.date('Ymd').'.txt','order->'.$out_trade_no.' message:'.$e->getMessage().$e->getFile().$e->getLine(),FILE_APPEND);
            $this->error($e->getMessage().$e->getLine().$e->getFile(),['code'=>'FAIL','message'=>'失败'],500);
        }
    }

    public function notifyAlipay()
    {

    }

}
